package com.mangguo.tingjian.statistics.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mangguo.tingjian.content.dto.ContentDataAudioTableResult;
import com.mangguo.tingjian.content.dto.ContentDataAuthorTableResult;
import com.mangguo.tingjian.content.mapper.DwPlayerMapper;
import com.mangguo.tingjian.statistics.dto.RetentionRateDTO;
import com.mangguo.tingjian.statistics.dto.RetentionRateLineQuery;
import com.mangguo.tingjian.statistics.entity.DwOpenApp;
import com.mangguo.tingjian.statistics.mapper.DwOpenAppMapper;
import com.mangguo.tingjian.statistics.req.RetentionRateLineReq;
import com.mangguo.tingjian.statistics.service.IDwOpenAppService;
import com.mangguo.tingjian.user.dto.CountResult;
import com.mangguo.tingjian.util.DateUtils;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author auto
 * @since 2019-12-01
 */
@Service
@Slf4j
@AllArgsConstructor
public class DwOpenAppServiceImpl extends ServiceImpl<DwOpenAppMapper, DwOpenApp> implements IDwOpenAppService {

    private DwOpenAppMapper dwOpenAppMapper;

    private DwPlayerMapper dwPlayerMapper;

    @Override
    public List<RetentionRateDTO> getRetentionRate(RetentionRateLineReq retentionRateLineReq) {
        Map<Integer, RetentionRateDTO> map = new ConcurrentHashMap<>();
        List<Integer> dateList = new LinkedList<>();
        try {
            dateList = DateUtils.getBetweenDates(retentionRateLineReq.getStart(), retentionRateLineReq.getEnd());
        } catch (Exception e) {
            log.error("获取时间区段异常", e);
        }

        dateList.forEach(dateInt -> {
            RetentionRateDTO retentionRateDTO = new RetentionRateDTO();
            retentionRateDTO.setMDate(dateInt);
            retentionRateDTO.setPeople(0L);
            retentionRateDTO.setRetentionD1("0.0000");
            retentionRateDTO.setRetentionD2("0.0000");
            retentionRateDTO.setRetentionD3("0.0000");
            retentionRateDTO.setRetentionD4("0.0000");
            retentionRateDTO.setRetentionD5("0.0000");
            retentionRateDTO.setRetentionD6("0.0000");
            retentionRateDTO.setRetentionD7("0.0000");
            retentionRateDTO.setRetentionD14("0.0000");
            retentionRateDTO.setRetentionD30("0.0000");
            map.put(dateInt, retentionRateDTO);
        });
        dwOpenAppMapper.getRetentionRateLines(retentionRateLineReq).forEach(retentionRateDTO -> {
            RetentionRateDTO retentionRate = map.get(retentionRateDTO.getMDate());
            retentionRate.setPeople(retentionRateDTO.getPeople());
            retentionRate.setRetentionD1(retentionRateDTO.getRetentionD1());
            retentionRate.setRetentionD2(retentionRateDTO.getRetentionD2());
            retentionRate.setRetentionD3(retentionRateDTO.getRetentionD3());
            retentionRate.setRetentionD4(retentionRateDTO.getRetentionD4());
            retentionRate.setRetentionD5(retentionRateDTO.getRetentionD5());
            retentionRate.setRetentionD6(retentionRateDTO.getRetentionD6());
            retentionRate.setRetentionD7(retentionRateDTO.getRetentionD7());
            retentionRate.setRetentionD14(retentionRateDTO.getRetentionD14());
            retentionRate.setRetentionD30(retentionRateDTO.getRetentionD30());
        });

        List<RetentionRateDTO> resultList = new LinkedList<>();
        dateList.forEach(dateInt -> resultList.add(map.get(dateInt)));
        return resultList;
//        List<RetentionRateDTO> retentionRateDTOS = new ArrayList<>();
//        Integer start = retentionRateLineReq.getStart();
//        Integer end = retentionRateLineReq.getEnd();
//
//
//
//        while (start <= end){
//            RetentionRateLineQuery query = new RetentionRateLineQuery();
//            query.setTableName(retentionRateLineReq.getTableName());
//            query.setMDate(start);
//            query.setMDate1(addDay(start, 1));
//            query.setMDate2(addDay(start, 2));
//            query.setMDate3(addDay(start, 3));
//            query.setMDate4(addDay(start, 4));
//            query.setMDate5(addDay(start, 5));
//            query.setMDate6(addDay(start, 6));
//            query.setMDate7(addDay(start, 7));
//            query.setMDate14(addDay(start, 14));
//            query.setMDate30(addDay(start, 30));
//            query.setCity(retentionRateLineReq.getCity());
//            query.setProvince(retentionRateLineReq.getProvince());
//            query.setOsid(retentionRateLineReq.getOsid());
//            query.setChannel(retentionRateLineReq.getChannel());
//
//            // 查人数
//            retentionRateLineReq.getChannel().remove("");
//            List<String> imeis = dwOpenAppMapper.getImeiBetweenDate(retentionRateLineReq, start);
//
//            RetentionRateDTO retentionRateDTO = new RetentionRateDTO();
//            retentionRateDTO.setMDate(start);
//            retentionRateDTO.setPeople((long) imeis.size());
//            if(imeis.size() > 0){
//                retentionRateDTO.setRetentionD1(String.format("%.2f", ((double)dwOpenAppMapper.countImeiByDateAndImei(query.getMDate1(), imeis)/imeis.size())));
//                retentionRateDTO.setRetentionD2(String.format("%.2f", ((double)dwOpenAppMapper.countImeiByDateAndImei(query.getMDate2(), imeis)/imeis.size())));
//                retentionRateDTO.setRetentionD3(String.format("%.2f", ((double)dwOpenAppMapper.countImeiByDateAndImei(query.getMDate3(), imeis)/imeis.size())));
//                retentionRateDTO.setRetentionD4(String.format("%.2f", ((double)dwOpenAppMapper.countImeiByDateAndImei(query.getMDate4(), imeis)/imeis.size())));
//                retentionRateDTO.setRetentionD5(String.format("%.2f", ((double)dwOpenAppMapper.countImeiByDateAndImei(query.getMDate5(), imeis)/imeis.size())));
//                retentionRateDTO.setRetentionD6(String.format("%.2f", ((double)dwOpenAppMapper.countImeiByDateAndImei(query.getMDate6(), imeis)/imeis.size())));
//                retentionRateDTO.setRetentionD7(String.format("%.2f", ((double)dwOpenAppMapper.countImeiByDateAndImei(query.getMDate7(), imeis)/imeis.size())));
//                retentionRateDTO.setRetentionD14(String.format("%.2f", ((double)dwOpenAppMapper.countImeiByDateAndImei(query.getMDate14(), imeis)/imeis.size())));
//                retentionRateDTO.setRetentionD30(String.format("%.2f", ((double)dwOpenAppMapper.countImeiByDateAndImei(query.getMDate30(), imeis)/imeis.size())));
//            }else{
//                retentionRateDTO.setRetentionD1("0.00");
//                retentionRateDTO.setRetentionD2("0.00");
//                retentionRateDTO.setRetentionD3("0.00");
//                retentionRateDTO.setRetentionD4("0.00");
//                retentionRateDTO.setRetentionD5("0.00");
//                retentionRateDTO.setRetentionD6("0.00");
//                retentionRateDTO.setRetentionD7("0.00");
//                retentionRateDTO.setRetentionD14("0.00");
//                retentionRateDTO.setRetentionD30("0.00");
//            }
//
//
//            retentionRateDTOS.add(retentionRateDTO);
//            start = addDay(start, 1);
//        }
//
//        return retentionRateDTOS;
    }

    @Override
    public List<CountResult> getUserPlayTime(Integer start, Integer end, Integer operateGroup) {
        return dwPlayerMapper.countPlayTimeByOperateGroup(start, end, operateGroup);
    }

    private Integer addDay(Integer dateLong, int day){

        Date date = null;
        try {
            date = new SimpleDateFormat("yyyyMMdd").parse(dateLong.toString());
        } catch (ParseException e) {
            log.error("日期转化错误", e);
        }

        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, day);
        return Integer.valueOf(new SimpleDateFormat("yyyyMMdd").format(calendar.getTime()));
    }

//
//    @Override
//    public List<RetentionRateDTO> getRetentionRateLine(RetentionRateLineReq retentionRateLineReq){
//        if(retentionRateLineReq.getMHourStart() == null || retentionRateLineReq.getMHourEnd() == null){
//            return Lists.newArrayList();
//        }
//        List<Integer> dayList = Lists.newArrayList();
//        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
//        try {
//            Date mTime = simpleDateFormat.parse(retentionRateLineReq.getMHourStart().toString());
//            Date mHourEndTime = simpleDateFormat.parse(retentionRateLineReq.getMHourEnd().toString());
//            Calendar calendar = Calendar.getInstance();
//            dayList.add(retentionRateLineReq.getMHourStart());
//            while (mTime.getTime() < mHourEndTime.getTime()){
//                calendar.setTime(mTime);
//                calendar.add(Calendar.DAY_OF_MONTH, 1);
//                mTime = calendar.getTime();
//                Integer timeCode = Integer.parseInt(simpleDateFormat.format(mTime));
//                dayList.add(timeCode);
//            }
//        }catch (Exception e){
//            log.error("getRetentionRateLine，时间转换异常, retentionRateLineReq:{}", JSONObject.toJSONString(retentionRateLineReq), e);
//        }
//        if(dayList == null || dayList.size() == 0){
//            return Lists.newArrayList();
//        }
//        List<RetentionRateDTO> retentionRateDTOS = Lists.newArrayList();
//        for(Integer timeCode : dayList){
//            try {
//                Date mTime = simpleDateFormat.parse(timeCode.toString());
//                Calendar calendar = Calendar.getInstance();
//                calendar.add(Calendar.DAY_OF_MONTH, 1);
//                Integer tomorrowCode = Integer.parseInt(simpleDateFormat.format(calendar.getTime()));
//
//                RetentionRateLineQuery query = new RetentionRateLineQuery();
//                query.setMDate(timeCode);
//                query.setMDate1(tomorrowCode);
//                query.setCity(retentionRateLineReq.getCityCode());
//                query.setProvince(retentionRateLineReq.getProvinceCode());
//                query.setPhoneType(retentionRateLineReq.getPhoneType());
//                query.setSubChannelId(retentionRateLineReq.getSubChannelId());
//
//                RetentionRateDTO retentionRateDTO = dwOpenAppMapper.getRetentionRateLines(query);
//                retentionRateDTOS.add(retentionRateDTO);
//            }catch (Exception e){
//                log.error("查找数据，时间转换异常, retentionRateLineReq:{}", JSONObject.toJSONString(retentionRateLineReq), e);
//            }
//        }
//        return retentionRateDTOS;
//    }

}
